<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="职阶名称" prop="jobName">
        <el-input
          v-model="queryParams.jobName"
          placeholder="请输入职阶名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流转顺序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入流转顺序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:stage:add']"
        >新增</el-button>
      </el-col>



      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="职阶名称" align="center" prop="jobName" />
      <el-table-column label="流转顺序" align="center" prop="sort" />
      <el-table-column label="备注" align="center" prop="bz" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:stage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['system:stage:view']"
          >详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:stage:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【在职阶段】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="职阶名称" prop="jobName">
          <el-input v-model="form.jobName" placeholder="请输入职阶名称" />
        </el-form-item>
        <el-form-item label="流转顺序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入流转顺序" />
        </el-form-item>
        <el-form-item label="备注" prop="bz">
          <el-input v-model="form.bz" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-table
          :data="timeShiftList"
          border
          @cell-click="cellDblClick"
          style="width: auto">
          <el-table-column label="班次名称" prop="name" :show-overflow-tooltip="true" width="240"/>
          <el-table-column label="价格(元)" prop="price" :show-overflow-tooltip="true" width="240">

            <template slot-scope="scope">
                          <span v-if="!scope.row.isEditCell" style="cursor:pointer">{{scope.row.price}}
                          </span>
              <el-input v-if="scope.row.isEditCell"
                        v-model="scope.row.price" placeholder="请输入价格"
                        @blur="cellBlur(scope.row,scope.column)"
                        style="width:70%" ref="priceRef"></el-input>
            </template>
            </el-table-column>

        </el-table>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 详情【在职阶段】对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="500px" append-to-body>
      <el-form ref="formView" :model="formView"   label-width="80px">
        <el-form-item label="职阶名称" prop="jobName">
          <el-input v-model="formView.jobName" :disabled="true" />
        </el-form-item>
        <el-form-item label="流转顺序" prop="sort">
          <el-input v-model="formView.sort" :disabled="true" />
        </el-form-item>
        <el-form-item label="备注" prop="bz">
          <el-input v-model="formView.bz"   :disabled="true"/>
        </el-form-item>

        <el-table
          :data="timeShiftList"
          border
          style="width: auto">
          <el-table-column label="班次名称" prop="name" :show-overflow-tooltip="true" width="240"/>
          <el-table-column label="价格(元)" prop="price" :show-overflow-tooltip="true" width="240">


          </el-table-column>

        </el-table>


      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import { listStage, getStage, delStage, addStage, updateStage, selectJobStageListById } from "@/api/job/jobStage/jobStage";
import {listTimeShiftList} from '@/api/job/jobset/jobSet'

export default {
  name: "Stage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【在职阶段】表格数据
      stageList: [],
      timeShiftList: [],
      tableData: [],
      priceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobName: null,
        sort: null,
        bz: null
      },
      // 表单参数
      form: {},
      formView: {},
      // 表单校验
      rules: {
        jobName: [
          { required: true, message: "职阶名称不能为空", trigger: "blur" }
        ],

        sort:[
          { required: true,type:'number',message: "流转顺序不能为空,且必须为数字值", trigger: "blur",transform: (value) => Number(value)}


        ],
      }
    };
  },
  created() {
    this.getList();
    this.listTimeShiftList();
  },
  methods: {
    /** 查询【在职阶段】列表 */
    getList() {
      this.loading = true;
      listStage(this.queryParams).then(response => {
        this.stageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        jobName: null,
        sort: null,
        bz: null,
        priceList:[]
      };

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
      this.timeShiftList=[];
      this.listTimeShiftList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      const id = row.id || this.ids
      getStage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
        let list =[];
        selectJobStageListById(id).then(response => {
          let data = response.rows;
          this.timeShiftList.forEach(function (item){
            data.forEach(function(d){
              if(item.id==d.timeShiftId){
                item.price=d.price;
                item.id = d.id;
                list.push(item.id+"_"+item.price);
              }
            })


          });

      });
        this.priceList=list;
      });
    },


    /** 新增按钮操作 */
    handleView(row) {
      this.reset();

      const id = row.id || this.ids
      getStage(id).then(response => {
        this.formView = response.data;
        this.openView = true;
        this.title = "详情";
        selectJobStageListById(id).then(response => {
          let data = response.rows;
          this.timeShiftList.forEach(function (item){
            data.forEach(function(d){
              if(item.id==d.timeShiftId){
                item.price=d.price;
              return;
              }
            })

          });

        });

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          if(this.priceList.length<this.timeShiftList.length){
             this.$alert("价格值为数字且大于0，不能为空，请核对！", "错误提示", {
              dangerouslyUseHTMLString: true
            });
            return;
          }
          this.form.priceList=this.priceList;
          if (this.form.id != null) {
            updateStage(this.form).then(response => {

              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStage(this.form).then(response => {

              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除【在职阶段】编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delStage(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 查询列表 */
    listTimeShiftList() {

      listTimeShiftList().then(
        response => {
          this.timeShiftList = response.rows;
          this.timeShiftList.forEach(function (item){
            item.price ="";
          });
        }
      );
    },

    // 双击编辑
    cellDblClick(row,column) {
      console.log(column.property)
      if (column.property == "price" ) {
        this.$set(row, "isEditCell", true);
      }
      this.tableData= this.tableData.filter(item => {
        return item;
      }) //视图刷新
      console.log(column.property)
      if (column.property == "price" ) {
        this.$nextTick(() => {
          this.$refs.priceRef.focus(); // 视图出现后使input获取焦点
        })
      }

    },
    // 可以编辑框失去焦点
    cellBlur(row, column) {
      row.isEditCell= false;
      this.$set(row, 'isEditCell', false);
      let id =row.id;
      let price =row.price;
      //校验位数字

      if (
        !Number(price) ||
        Number(price) <= 0
      ) {
        this.$alert("价格值为数字且大于0", "错误提示", {
          dangerouslyUseHTMLString: true
        });
        return;
      }
      let flag =0;
      this.priceList.forEach(function(item){
        let arr = item.split("_");
        if(arr[0]==row.id){
          item =id+"_"+price;
          flag =1;
        }
      })
      if(flag ==0){
        this.priceList.push(id+"_"+price);
      }

    },




  }
};
</script>
